/**
 * Created by gshg on 2017/7/26.
 */


//  检查身份证号码是否符合规则
function proIDcard(sId){
    sId =  sId.replace(/(^\s*)|(\s*$)/g,"")
    if (sId == null || sId == "") {
        return false;
    }
    var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
    var iSum=0 ;
    var info="" ;
    if(!/^\d{17}(\d|x)$/i.test(sId)) {
        return false;
    }
    sId=sId.replace(/x$/i,"a");
    if(aCity[parseInt(sId.substr(0,2))]==null) {
        return false;

    }
    var sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
    var d=new Date(sBirthday.replace(/-/g,"/")) ;
    if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate())){
        return false;
    }
    /*身份证号码校验位检查*/
    for(var i = 17;i>=0;i --) {
        iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11) ;
    }
    if(iSum%11!=1) {
        return false;
    }
    return true;
}


//统一社会信用代码校验规则
function CheckSocialCreditCode(code) {
    code =  code.replace(/(^\s*)|(\s*$)/g,"")
    var testCode = numAndupperCaseCheck(code);
    if ((code.length != 18) || (testCode == false)) {
        return false;
    }else{
        var Ancode;//统一社会信用代码的每一个值
        var Ancodevalue;//统一社会信用代码每一个值的权重
        var total = 0;
        var weightedfactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];//加权因子
        var str = '0123456789ABCDEFGHJKLMNPQRTUWXY';//不用I、O、S、V、Z
        for (var i = 0; i < code.length - 1; i++){
            Ancode = code.substring(i, i + 1);
            Ancodevalue = str.indexOf(Ancode);
            total = total + Ancodevalue * weightedfactors[i];//权重与加权因子相乘之和
        }
        var logiccheckcode = 31 - total % 31;
        if (logiccheckcode == 31){
            logiccheckcode = 0;
        }
        var Str = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,T,U,W,X,Y";
        var Array_Str = Str.split(',');
        logiccheckcode = Array_Str[logiccheckcode];
        var checkcode = code.substring(17, 18);
        if (logiccheckcode != checkcode){
            return false;
        }
        return true;
    }

}

/**
 * 只允许数字和大写字母
 * @param code
 * @returns {boolean}
 */
function numAndupperCaseCheck(code){
    code =  code.replace(/(^\s*)|(\s*$)/g,"");
    if(code == "" || code.length==0){
        return false;
    }
    var patrn = /^[0-9A-Z]+$/;
    return patrn.test(code);
}

//组织机构代码校验
function checkorgcode(orgcode){
    orgcode =  orgcode.replace(/(^\s*)|(\s*$)/g,"");
    if(orgcode.length!=9){
        return false;
    }

    var patrn=/^[0-9A-Z]+$/;

    if(patrn.test(orgcode)==false){
        return false;
    }

    var lastpatrn=/^[0-9X]+$/;
    var checkcode=orgcode.substring(8,9);
    if(lastpatrn.test(checkcode)==false){//最后的校验为只能是0到9的数字或者大写字母X
        return false;
    }
    var ws = [3, 7, 9, 10, 5, 8, 4, 2];
    var str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var ancode;
    var ancodevalue;
    var total=0;
    for(var i=0;i<orgcode.length-1;i++){
        ancode=orgcode.substring(i,i+1);
        ancodevalue=str.indexOf(ancode) * ws[i];
        total=total+ancodevalue;
    }
    var logiccheckcode=11-total%11;
    if(logiccheckcode==10){
        logiccheckcode='X';
    }
    if(logiccheckcode==11){
        logiccheckcode='0';
    }

    if(checkcode != logiccheckcode){
        return false;
    } else{
        return true;
    }

}

//工商营业执照
function isValidBusCode(busCode){
    busCode =  busCode.replace(/(^\s*)|(\s*$)/g,"");
    var ret=false;
    if(""==busCode || busCode.length == 0){
        return false;
    }
    var patrn = /^[0-9]+$/;
    if(!patrn.test(busCode)){
        return false;
    }
    if(busCode.length==15){
        var sum=0;
        var s=[];
        var p=[];
        var a=[];
        var m=10;
        p[0]=m;
        for(var i=0;i<busCode.length;i++){
            a[i]=parseInt(busCode.substring(i,i+1),m);
            s[i]=(p[i]%(m+1))+a[i];
            if(0==s[i]%m){
                p[i+1]=10*2;
            }else{
                p[i+1]=(s[i]%m)*2;
            }
        }
        if(1==(s[14]%m)){
            //营业执照编号正确!
            ret=true;
        }else{
            //营业执照编号错误!
            ret=false;

        }
    } else{
        ret=false;
        //alert("营业执照格式不对，必须是15位数的！");
    }
    return ret;
}

function numAndLetter(idNumber){
    ///^[0-9a-zA-Z]*$/g;
    idNumber =  idNumber.replace(/(^\s*)|(\s*$)/g,"");
    if(idNumber == "" || idNumber.length==0){
        return false;
    }
    var patrn = /^[0-9a-zA-Z]*$/g;
    return patrn.test(idNumber);
}

function checkIdNumber(idNumber, type){
    var result = false;
    if(type=='10'){
        result = proIDcard(idNumber);
    }else if(type=='49'){
        result =  CheckSocialCreditCode(idNumber);
    }else if(type=='50'){
        result =  checkorgcode(idNumber);
    }else if(type=='51'){
        result =  isValidBusCode(idNumber);
    }else if(type=='60'){
        result =  numAndupperCaseCheck(idNumber);
    }else{
        result =  numAndLetter(idNumber);
    }
    return result;
}